package com.zkingsoft.zkingbase.holder;

import android.content.Context;
import android.support.annotation.LayoutRes;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.zkingsoft.zkingbase.R;

import java.io.File;


/**
 * Created by ytf on 2016/08/10.
 * Description: 基础viewholder
 * Modify by xx on 2016/08/10.
 * Modify detail:
 */
public abstract class BaseViewHolder<T> {
    protected T data;
    protected View v;
    protected Context mContext;

    public BaseViewHolder(Context context){
        this.mContext = context;
        init(context);
    }
    
    public BaseViewHolder(T data){
        this.data = data;
        init(mContext);
    }

    private void init(Context context){
        int layout = getViewLayout();
        v = View.inflate(context, layout, null);
        v.setTag(this);
    }

    /***
     * 将item的视图传入待解析
     * @return item的布局
     */
    protected abstract @LayoutRes int getViewLayout();

    public void setData(T d, int position){
        this.data = d;
        refreshView(position);
    }

    /***
     *
     * @return 返回contentView，即item的布局
     */
    public View getContentView(){
        return v;
    }

    /***
     * 数据显示更新调用此方法完成
     */
    protected abstract void refreshView(int position);

    public void showImg(Context context, String url, ImageView iv)
    {
        if(url.startsWith("file:"))
        {
            Glide.with(context)
                    .load(new File(url))
                    .error(R.drawable.loading)
                    .placeholder(R.drawable.loading)
                    .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                    .into(iv);
        }else
        {
            Glide.with(context)
                    .load(url)
                    .error(R.drawable.loading)
                    .placeholder(R.drawable.loading)
                    .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                    .into(iv);
        }
    }
}
